;(function(w){
    "use strict";

    if (w['embedear']) {
        return false;
    }

    var self = 'base.js',

    docHead = null,
    root = null,
    loadFiles = new Object(),

    baseClass = {
        protocol: ('https:' == w.location.protocol ? 'https' : 'http'),

        getElements: (function(d){
            if ('function' === typeof d.querySelectorAll) {
                return function (className, callback) {
                    var className = '.'+className.replace(/^\./, ""),
                    callback = ('function' === typeof callback ? callback : false),
                    elements = d.querySelectorAll(className),
                    results = [],
                    i = 0, l = elements.length, c = 0,
                    current;
                    for (; i < l; i++) {
                        current = elements[i];
                        results.push(current);
                        if (callback) {
                            callback(c, current, (i+1 == l));
                        }
                        c++;
                    }
                    return results;
                }
            }

            else if ('function' === typeof d.getElementsByClassName) {
                return  function (className, callback) {
                    var className = className.replace(/^\./, ""),
                    callback = ('function' === typeof callback ? callback : false),
                    elements = d.getElementsByClassName(className),
                    results = new Array(),
                    i = 0, l = elements.length, c = 0,
                    current;
                    for (; i < l; i++) {
                        current = elements[i];
                        results.push(current);
                        if (callback) {
                            callback(c, current, (i+1 == l));
                        }
                        c++;
                    }
                    return results;
                }
            }

            else if ('function' === typeof d.evaluate) {
                return  function (className, callback) {
                    var className = className.replace(/^\./, ""),
                    callback = ('function' === typeof callback ? callback : false),
                    elements = d.evaluate('//*[contains(concat(" ",normalize-space(@class)," "),"'+className+'")]', d, null, 0, null),
                    results = new Array(),
                    i = 0, l = elements.length, c = 0,
                    current;
                    while (current = elements.iterateNext()) {
                        results.push(current);
                        if (callback) {
                            callback(c, current, (i+1 == l));
                        }
                        c++;
                    }
                    return results;
                }
            } else {
                return  function (className, callback) {
                    var className = className.replace(/^\./, ""),
                    callback = ('function' === typeof callback ? callback : false),
                    elements = d.all || d.getElementsByTagName('*'),
                    results = new Array(),
                    i = 0, l = elements.length, c = 0,
                    current;
                    for (; i < l; i++) {
                        current = elements[i];
                        if (current.className.indexOf(className) != -1) {
                            results.push(current);
                            if (callback) {
                                callback(c, current, (i+1 == l));
                            }
                            c++;
                        }
                    }
                    return results;
                }
            }
        })(document),

        getJson: function(url, callback){
            if ('function' !== typeof callback) {
                return false;
            }

            if (loadFiles[url]) {
                callback(loadFiles[url]);
            } else {
                var xhr = new XMLHttpRequest();
                xhr.onreadystatechange = function (){
                    if (4 === xhr.readyState && 200 === xhr.status) {
                        var data = JSON.parse(xhr.responseText);
                        loadFiles[url] = data;
                        callback(data);
                    }
                };
                xhr.open('GET', url, true);
                xhr.send(null);
            }
        },

        loadCss: function(path) {
            path = root + path;

            if (!loadFiles[path]) {
                var css = document.createElement('link');
                css.rel = 'stylesheet';
                css.href = path;
                if (!docHead) {
                    docHead = document.getElementsByTagName('head')[0];
                }
                docHead.appendChild(css);
                loadFiles[path] = true;
            }
        },

        dateFormat: function(date, format) {
            format = format||'YYYY/MM/DD hh:mm';
            date = new Date(date);
            return format.replace(/YYYY/g, date.getFullYear())
                .replace(/MM/g, ('0' + (date.getMonth() + 1)).slice(-2))
                .replace(/DD/g, ('0' + date.getDate()).slice(-2))
                .replace(/hh/g, ('0' + date.getHours()).slice(-2))
                .replace(/mm/g, ('0' + date.getMinutes()).slice(-2));
        }
    };

    var elements = document.getElementsByTagName('script'),
    i = 0,
    l = elements.length,
    el, src, pos;
    for (; i < l; i++) {
        el = elements[i];
        src = el.src;
        if (src.indexOf('embedear') != -1 && src.indexOf(self) != -1) {
            pos = src.indexOf('?');
            if (pos != -1) {
                w['embedear'] = baseClass;
                root = src.substr(0, src.indexOf(self));

                var names = src.substr(pos + 1).split(','), js;
                l = names.length;
                for (i = 0; i < l; i++) {
                    js = document.createElement('script');
                    js.type = 'text/javascript';
                    js.async = true;
                    js.src = root + names[i] + '.js';
                    el.parentNode.insertBefore(js, el);
                }
                break;
            }
            
        }
    }
})(window);